package com.imgo.max.fans.service;

import com.alibaba.fastjson.JSON;
import com.imgo.max.artist.dto.CancelCollectHisDto;
import com.imgo.max.artist.redis.FansRedisDao;
import com.imgo.max.artist.service.FansManageService;
import com.imgo.max.artist.service.FansMessageService;
import com.imgo.max.common.dto.ResultDO;
import com.imgo.max.common.log.CustomLogger;
import com.imgo.max.common.log.CustomLoggerFactory;
import com.imgo.max.common.result.FansResultCodeEnum;
import com.imgo.max.common.result.ModuleResultCode;
import com.imgo.max.common.util.LoggerUtils;
import com.imgo.max.fans.dao.FocusArtistDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service("fansManageService")
public class FansManageServiceImpl extends FansBaseServiceImpl implements FansManageService {
    private static final CustomLogger logger = CustomLoggerFactory.getLogger(FansManageServiceImpl.class);

    @Resource
    private FocusArtistDao focusArtistDao;
    @Resource
    private FansMessageService fansMessageService;
    @Resource
    private FansRedisDao fansRedisDao;

    /**
     * 后台取消关注接口：
     */
    @Transactional
    @Override
    public ResultDO cancelCollectArtistManage(String uid, String artistUid) {
        ResultDO resultDO = ResultDO.buildResult();
        if (StringUtils.isBlank(uid) || StringUtils.isBlank(artistUid)) {
            setResultDOResultCode(resultDO, FansResultCodeEnum.PARAM_IS_EMPTY_ERROR);
            return resultDO;
        }

        try {
            fansMessageService.cancelCollectArtist(uid, artistUid);
            Integer fansNum = fansRedisDao.getFansNum(artistUid);
            fansNum = fansNum - 1;
            if (fansNum < 0) {
                fansNum = 0;
            }
            resultDO.setModule(fansNum);
        } catch (Exception e) {
            LoggerUtils.errorAndResultDO(logger, e, ModuleResultCode.FANS, resultDO, uid, artistUid);
        }
        return resultDO;
    }

    /**
     * 取消关注记录到历史表中
     *
     * @param cancelCollectHisDto
     * @return ResultDO
     */
    @Override
    @Transactional
    public ResultDO insertCancelCollectHisByDto(CancelCollectHisDto cancelCollectHisDto) {
        ResultDO resultDO = ResultDO.buildResult();
        try {
            resultDO = fansMessageService.insertCancelCollectHisByDto(cancelCollectHisDto);
        } catch (Exception e) {
            LoggerUtils.errorAndResultDO(logger, e, ModuleResultCode.FANS, resultDO, JSON.toJSONString(cancelCollectHisDto));
        }
        return resultDO;
    }

    /**
     * 取消用户关注所有艺人
     *
     * @param uid
     * @return
     */
    @Override
    public ResultDO cancelCollectArtists(String uid) {
        ResultDO resultDO = ResultDO.buildResult();
        if (StringUtils.isBlank(uid)) {
            setResultDOResultCode(resultDO, FansResultCodeEnum.PARAM_IS_EMPTY_ERROR);
            return resultDO;
        }
        try {
            resultDO = fansMessageService.cancelCollectArtists(uid);
        } catch (Exception e) {
            LoggerUtils.errorAndResultDO(logger, e, ModuleResultCode.FANS, resultDO, uid);
        }
        return resultDO;
    }

    /**
     * 查询用户关注所有艺人
     *
     * @param uid
     * @return total
     */
    @Override
    public ResultDO getQueryCollectArtistCountByUid(String uid) {
        ResultDO resultDO = ResultDO.buildResult();
        if (StringUtils.isBlank(uid)) {
            setResultDOResultCode(resultDO, FansResultCodeEnum.PARAM_IS_EMPTY_ERROR);
            return resultDO;
        }
        try {
            Integer total = focusArtistDao.getQueryCollectArtistCountByUid(uid);
            resultDO.setModule(total);
        } catch (Exception e) {
            LoggerUtils.errorAndResultDO(logger, e, ModuleResultCode.FANS, resultDO, uid);
        }
        return resultDO;

    }

    /**
     * 返回指定艺人特定时间段内的粉丝数量
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param artistId  艺人ID
     * @return
     */
    @Override
    public ResultDO<Integer> getFansNumAtCertainTimes(String artistId, Date startTime, Date endTime) {
        ResultDO<Integer> resultDO = ResultDO.buildResult();
        if (startTime.after(endTime)) {
            setResultDOResultCode(resultDO, FansResultCodeEnum.QUERY_PARAMS_DATE_ERROR);
            return resultDO;
        }
        int num = 0;
        try {
            num = focusArtistDao.getFansNumAtCertainTimes(startTime, endTime, artistId);
        } catch (Exception e) {
            LoggerUtils.error(logger, e, ModuleResultCode.FANS, artistId, startTime, endTime);
        }
        resultDO.setModule(num);
        return resultDO;
    }

}
